package com.java.controller;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.java.entity.AccountInfo;
import com.java.entity.event.AccountInfoEvent;
import com.java.entity.event.ShareInfoEvent;
import com.java.service.AccountService;
import com.java.service.GuidanceService;
import com.java.service.ShareService;
import com.java.service.SugarService;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired private AccountService accountService;
	@Autowired private SugarService sugarService;
	@Autowired private ShareService shareService;
	@Autowired GuidanceService guidanceService;
	
	@PostMapping("/getAccount")
	public AccountInfoEvent getAccount(
			@RequestParam(required = false) String accountId) {
		accountId = ServletUtil.getUserId();
		return accountService.getAccount(accountId);
	}
	
	@PostMapping("/saveAccount")
	public BaseRespone saveAccount(@RequestBody AccountInfo account) {
		account.setAccountId(ServletUtil.getUserId());
		return accountService.update(account);
	}
	
	@PostMapping("/getSugarRecords")
	public TableRespone getSugarRecords(
			@RequestParam(required = false) String accountId) {
		accountId = ServletUtil.getUserId();
		return sugarService.getSugarList(accountId,0, 0);
	}
	
	@PostMapping("/getSugarRecord")
	public BaseRespone getSugarRecord(
			@RequestParam(required = false) String accountId,
			@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
		accountId = ServletUtil.getUserId();
		return sugarService.getSugarRecord(accountId, date);
	}
	
	@PostMapping("/saveSugarRecord")
	public BaseRespone saveSugarRecord(
			@RequestParam(required = false) String accountId,
			BigDecimal sugarValue,
			@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
		accountId = ServletUtil.getUserId();
		return sugarService.saveSugarRecord(accountId, sugarValue, date);
	}
	
	@PostMapping("/getShares")
	public TableRespone getShares(
			@RequestParam(required = false, defaultValue = "") String accountId,
			@RequestParam(required = false, defaultValue = "") String title,
			@RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		return shareService.getShareList(accountId, title, page, size);
	}
	
	@PostMapping("/getShare")
	public ShareInfoEvent getShare(
			@RequestParam(required = true) String shareId) {
		return shareService.getShare(shareId);
	}

	@PostMapping("/saveShare")
	public BaseRespone saveShare(@RequestBody ShareInfoEvent shareInfo) {
		String accountId = ServletUtil.getUserId();
		return shareService.saveShare(null, accountId, shareInfo.getShareTitle(), shareInfo.getShareSubtitle(), shareInfo.getShareText());
	}

	@PostMapping("/delShare")
	public BaseRespone delShare(@RequestParam(required = false) String shareId) {
		return shareService.delShare(shareId);
	}

	@PostMapping("/sendGuidance")
	public BaseRespone sendGuidance(
			@RequestParam(required = true) String receiveId,
			@RequestParam(required = true) String text) {
		String accountId = ServletUtil.getUserId();
		return guidanceService.sendGuidance(accountId, receiveId, text);
	}
	
	@PostMapping("/getGuidancesBySend")
	public TableRespone getGuidancesBySend(
			@RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		String accountId = ServletUtil.getUserId();
		return guidanceService.getGuidancesBySend(accountId, page, size);
	}
	
	@PostMapping("/getGuidancesByReceive")
	public TableRespone getGuidancesByReceive(
			@RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		String accountId = ServletUtil.getUserId();
		return guidanceService.getGuidancesByReceive(accountId, page, size);
	}

	@PostMapping("/delGuidance")
	public BaseRespone delGuidance(
			@RequestParam(required = true) String guidanceId) {
		return guidanceService.delGuidance(guidanceId);
	}

	@PostMapping("/replyGuidance")
	public BaseRespone replyGuidance(
			@RequestParam(required = true) String guidanceId,
			@RequestParam(required = true) String guidanceReply) {
		return guidanceService.replyGuidance(guidanceId, guidanceReply);
	}
	
}
